﻿using HandyControl.Controls;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace LpbPrj.Client.Controls;

public class PropertyExResolver : PropertyResolver
{
    /// <summary>
    /// 获取显示顺序
    /// </summary>
    /// <param name="propertyDescriptor"></param>
    /// <returns></returns>
    public int ResolveDisplayOrderValue(PropertyDescriptor propertyDescriptor)
    {
        var defaultValueAttribute = propertyDescriptor.Attributes.OfType<DisplayOrderAttribute>().FirstOrDefault();
        return defaultValueAttribute == null ? int.MaxValue : defaultValueAttribute.Order;
    }

    /// <summary>
    /// 获取分组顺序
    /// </summary>
    /// <param name="propertyDescriptor"></param>
    /// <returns></returns>
    public int ResolveCategoryOrderValue(PropertyDescriptor propertyDescriptor)
    {
        var defaultValueAttribute = propertyDescriptor.Attributes.OfType<CategoryOrderAttribute>().FirstOrDefault();
        return defaultValueAttribute == null ? int.MaxValue : defaultValueAttribute.Order;
    }

    /// <summary>
    /// 获取组别名称
    /// </summary>
    /// <param name="propertyDescriptor"></param>
    /// <returns></returns>
    public string ResolveCategorNameValue(PropertyDescriptor propertyDescriptor)
    {
        var defaultValueAttribute = propertyDescriptor.Attributes.OfType<CategoryOrderAttribute>().FirstOrDefault();
        return defaultValueAttribute == null ? "其他" : defaultValueAttribute.Category;
    }


    /// <summary>
    /// 根据属性类型展示不同的控件
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public override PropertyEditorBase CreateDefaultEditor(Type type)
    {
        Debug.WriteLine(type.ToString());

        //string
        if (type == typeof(string))
        {
            return new StringPropertyEditor();
        }

        //Thickness
        if (type == typeof(Thickness))
        {
            return new ThicknessLeftTopPropertyEditor();
        }

        //ImageSource
        if (type == typeof(ImageSource) || type == typeof(BitmapFrame))
        {
            return new ImageSourcePropertyEditor();
        }

        //ImageBrush
        if (type == typeof(ImageBrush))
        {
            return new ImageBrushPropertyEditor();
        }

        //Uri
        if (type == typeof(Uri))
        {
            return new ImageUriPropertyEditor();
        }

        //SolidColorBrush
        if (type == typeof(SolidColorBrush))
        {
            return new SolidColorBrushPropertyEditor();
        }

        return base.CreateDefaultEditor(type);

    }

}
